package com.example.htmlutildemo.util;

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

public class HttpUtil {
    public String getBaiDu( ) {
        try {
            CloseableHttpClient httpClient = HttpClients.createDefault();
//            HttpGet httpGet = new HttpGet("https://cn.bing.com/th?id=OHR.PalacioArtes_EN-CN8166631149_1920x1080.jpg&rf=LaDigue_1920x1080.jpg");
            HttpGet httpGet = new HttpGet("https://cn.bing.com");
            CloseableHttpResponse execute = httpClient.execute(httpGet);
            HttpEntity entity = execute.getEntity();


//            byte[] bytes = EntityUtils.toByteArray(entity);
//            FileOutputStream stream = new FileOutputStream("D:/aaaa.jpg");
//
//            stream.write(bytes);
//            stream.close();


            String s = EntityUtils.toString(entity, StandardCharsets.UTF_8);
            System.out.println(s);

            execute.close();
            httpClient.close();
            return "s";
        } catch (IOException e) {
            return "";
        }
    }

    public static void main(String[] args) {
        new HttpUtil().getBaiDu();
    }
}
